package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Movies;
import com.itheima.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin(value = {"http://localhost:81"})// 跨域支持
@RestController
@RequestMapping("/movies")
public class MoviesController {

    @Autowired
    private MoviesService moviesService;

    // 分页查询方法
    @GetMapping("/findByPage")
    public PageInfo<Movies> findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Integer cid,
            String keyword) {
        // 调用service查询
        return moviesService.findByPage(pageNum, pageSize, cid, keyword);
    }

    // 保存
    @PostMapping("/save")
    public String save(@RequestBody Movies movies) {

        // 判断id是否存在
        if (movies.getId()!=null) {
            // 修改
            moviesService.update(movies);
        }else{
            // 新增
            moviesService.save(movies);
        }

        return "ok";
    }

    // 根据id查询
    @GetMapping("/findById")
    public Movies findById(Integer id){
        return moviesService.findById(id);
    }
}
